// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html


// import {EEntityID} from "./Defines";
import {EntityMgr} from "./EntityManager";
import {Telegram} from "../message/Telegram";

const {ccclass, property} = cc._decorator;

@ccclass
export abstract class BaseGameEntity extends cc.Component {

    private static NextValidId:number = 0;

    @property
    private _Id:number = 0;

    @property
    get id():number {
        return this._Id;
    }
    set id(val:number) {
        this.setId(val);
        // this._id = val;
    }
    

    private _hbElapsed:number = 0;

    // constructor(...params) {
    //     super();
    //     console.log(params);
    //     // this.setId(id);
    // }

    private setId(id:number) {
        if(id < BaseGameEntity.NextValidId)
            throw new Error(`<BaseGameEntity::SetID>: invalid ID`);

        this._Id = id;
        BaseGameEntity.NextValidId = this._Id + 1;
    }

    onLoad() {
        EntityMgr.registerEntity(this);
    }

    onDestroy() {
        EntityMgr.removeEntity(this);
    }

    update(dt:number) {
        this._hbElapsed += dt;
        if(this._hbElapsed >= 1.8) {
            this.heartbeat();
            this._hbElapsed -= 1.8;
        }
    }

    abstract heartbeat();

    abstract handleMessage(t:Telegram);
}